package com.example.glsurfaceview;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

import com.example.renderer.ScreenRenderer;
import com.example.state.AppState;

/**
 * Created by Lucas on 20/06/13.
 */
public class MyGLSurfaceView extends GLSurfaceView {

    /**
     * The renderer of the application
     */
    private ScreenRenderer _renderer;
    /**
     * The current state of the application
     */
    private AppState _state;
    /**
     * The debug TAG
     */
    private final static String TAG = "MyGLSurfaceView";

    /**
     * Constructor of the GLSurfaceView
     * @param context the context of the application
     */
    public MyGLSurfaceView(Context context) {
        super(context);
        Log.i(TAG, "GLSurfaceView constructor");
        setEGLConfigChooser(8, 8, 8, 8, 0, 0);
        getHolder().setFormat(PixelFormat.RGBA_8888);
        _renderer = new ScreenRenderer(context);

        setEGLContextClientVersion(2);
        setRenderer(_renderer);
        // Render the view only when there is a change in the drawing data
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    /**
     * called every time a user touch the screen
     * @param e the input event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return _state.onTouchEvent(e);
    }

    /**
     * change the current state of the application
     * @param currentState the new current state
     */
    public void setState(AppState currentState) {
        Log.i(TAG, "setState");
        _state = currentState;
        _renderer.setAppState(currentState);
    }

    /**
     * Change the view of the renderer
     * @param delta
     */
    public void changeView(float delta) {
        _renderer.changeView(delta);
    }
}
